package com.yc.framework.utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class MockRegister {

    public static String encodePwd(String pwd) {
        return new BCryptPasswordEncoder().encode(pwd);
    }

    public static void main(String[] args) throws SQLException {

        String url = "jdbc:mysql://localhost:3306/used_books?serverTimezone=GMT";
        String userName = "root";
        String pwd = "root";
        Connection connection = null;
        try {
            // 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 创建数据库连接
            connection = DriverManager.getConnection(url, userName, pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }

        PreparedStatement ps = null;
        try {
            ps = connection.prepareStatement("INSERT INTO t_user(username, password) VALUES (?,?)");
            ps.setString(1,"user2");
            ps.setString(2,encodePwd("123456"));
            ps.addBatch();
//            ps.setString(1,"reader");
//            ps.setString(2,encodePwd("reader"));
//            ps.addBatch();
            ps.executeBatch();

//            ps = connection.prepareStatement("INSERT INTO user_role(username, role) VALUES (?,?)");
//            ps.setString(1,"spring");
//            ps.setString(2,"ROLE_admin");
//            ps.addBatch();
//            ps.setString(1,"reader");
//            ps.setString(2,"ROLE_reader");
//            ps.addBatch();
//            ps.executeBatch();


        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(ps!=null){
                ps.close();
            }

            if(connection!=null){
                connection.close();
            }
        }

    }
}

